package com.raceway.ihcenterapp.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.raceway.ihcenterapp.domain.Audio;
import com.raceway.ihcenterapp.domain.GPS;
import com.raceway.ihcenterapp.domain.HelpRequest;
import com.raceway.ihcenterapp.domain.IHS;
import com.raceway.ihcenterapp.domain.Person;
import com.raceway.ihcenterapp.domain.Picture;
import com.raceway.ihcenterapp.domain.UserRole;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RooWebScaffold(path = "pictures", formBackingObject = Picture.class,update=false,delete=false,create=false)
@RequestMapping("/pictures")
@Controller
public class PictureController {
	
	UserDetails user;
	Collection<GrantedAuthority> authorities;
	Iterator<GrantedAuthority> roles;
	String role;
	UserRole  userRoles;
	Person person;
	@PersistenceContext
	private EntityManager em;
	
    @RequestMapping(value = "/findhelprequestpictures/{id}", method = RequestMethod.GET)
    public String audiosOfHelpRequest(@PathVariable("id") Long id, Model model) {

        HelpRequest helpRequest=HelpRequest.findHelpRequest(id);

        model.addAttribute("pictures", Picture.findPicturesByHelpRequestId(helpRequest).getResultList());
        return "pictures/list";
    }

	@RequestMapping(method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model model) {
		user=(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		authorities=user.getAuthorities();
		roles=authorities.iterator();
		role=roles.next().toString();
		
		
		if(role.equalsIgnoreCase(userRoles.ROLE_IHS_USER.toString())){
			Query query = Person.findPeopleByUserName(user.getUsername());
			person = (Person) query.getSingleResult();
			IHS ihs=person.getWorkedIHS();
			List pictures=new ArrayList<Picture>();
			query=em.createQuery("SELECT Picture FROM Picture picture JOIN picture.HelpRequestId helprequest WHERE helprequest.assignedASI1=:ihs",Picture.class);
			query.setParameter("ihs", ihs);
			pictures=query.getResultList();
			 model.addAttribute("pictures", query.getResultList());
			
		}else{
		
		
		if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            model.addAttribute("pictures", Picture.findPictureEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
            float nrOfPages = (float) Picture.countPictures() / sizeNo;
            model.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            model.addAttribute("pictures", Picture.findAllPictures());
        }
        addDateTimeFormatPatterns(model);
		}
        return "pictures/list";
    }
}
